package view_server

import (
	"bigdevops/src/cache"
	"bigdevops/src/common"
	"bigdevops/src/config"
	"bigdevops/src/models"
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sort"
)

// 这个是集群 ns联动使用的
func getClusterNamespacesForCascader(c *gin.Context) {
	//sc := c.MustGet(common.GIN_CTX_CONFIG_CONFIG).(*config.ServerConfig)

	// 从cache中通过 集群的id 拿到集群的clientSet
	kc := c.MustGet(common.GIN_CTX_K8s_CACHE).(*cache.K8sClusterCache)
	nsMap := kc.GetClusterNamespaceMap()

	res := []*common.FrontLabelOption{}
	clusterNames := []string{}
	for clusterName := range nsMap {

		clusterNames = append(clusterNames, clusterName)
	}
	sort.Strings(clusterNames)

	for _, clusterName := range clusterNames {
		nsNames := nsMap[clusterName]
		one := &common.FrontLabelOption{
			Label: clusterName,
			Value: clusterName,
		}

		nsChildren := []*common.FrontLabelOption{}

		nsAll := &common.FrontLabelOption{
			Label: "所有ns",
			Value: common.K8S_NS_ALL_KEY,
		}
		nsChildren = append(nsChildren, nsAll)
		for _, nsName := range nsNames {
			oneNs := &common.FrontLabelOption{
				Label: nsName,
				Value: nsName,
			}
			nsChildren = append(nsChildren, oneNs)

		}
		one.Children = nsChildren

		res = append(res, one)
	}

	common.OkWithDetailed(res, "ok", c)
}

// 这个是给混合所有集群的ns 去重
func getClusterNamespacesUnique(c *gin.Context) {
	//sc := c.MustGet(common.GIN_CTX_CONFIG_CONFIG).(*config.ServerConfig)

	// 从cache中通过 集群的id 拿到集群的clientSet
	kc := c.MustGet(common.GIN_CTX_K8s_CACHE).(*cache.K8sClusterCache)
	nsMap := kc.GetClusterNamespaceMap()

	res := []*common.FrontLabelOption{}
	tmpM := map[string]string{}
	for _, nss := range nsMap {
		for _, ns := range nss {
			tmpM[ns] = ""
		}
	}
	finalRes := []string{}
	for ns := range tmpM {
		finalRes = append(finalRes, ns)
	}

	sort.Strings(finalRes)

	for _, ns := range finalRes {
		one := &common.FrontLabelOption{
			Label: ns,
			Value: ns,
		}

		res = append(res, one)
	}

	common.OkWithDetailed(res, "ok", c)
}

// 这个是集群 选择ns
func getClusterNamespacesForSelect(c *gin.Context) {
	sc := c.MustGet(common.GIN_CTX_CONFIG_CONFIG).(*config.ServerConfig)

	// 集群的名称
	searchCluster := c.DefaultQuery("cluster", "")

	if searchCluster == "" {
		msg := "集群名称未传入"
		sc.Logger.Error(msg)
		common.ReqBadFailWithMessage(msg, c)
		return
	}

	dbObj, err := models.GetK8sClusterByName(searchCluster)
	if err != nil {
		sc.Logger.Error("根据name找k8s集群错误", zap.Any("k8s集群配置", searchCluster), zap.Error(err))
		common.FailWithMessage(err.Error(), c)
		return
	}

	// 从cache中通过 集群的id 拿到集群的clientSet
	kc := c.MustGet(common.GIN_CTX_K8s_CACHE).(*cache.K8sClusterCache)
	kSet := kc.GetClusterClientSetById(dbObj.ID)
	if kSet == nil {
		msg := fmt.Sprintf("根据id找k8s集群kset缓存错误")
		sc.Logger.Error(msg, zap.Any("k8s集群配置", searchCluster), zap.Error(err))
		common.FailWithMessage(msg, c)
		return
	}

	ctx1, cancel1 := common.GenTimeoutContext(dbObj.ActionTimeoutSeconds)
	defer cancel1()
	nsItems, err := kSet.CoreV1().Namespaces().List(ctx1, metav1.ListOptions{})
	if err != nil {
		msg := fmt.Sprintf("获取ns错误:%v", err.Error())
		sc.Logger.Error(msg, zap.Any("k8s集群配置", searchCluster), zap.Error(err))
		common.FailWithMessage(msg, c)
		return
	}

	res := []*common.FrontLabelOption{}
	for _, ns := range nsItems.Items {
		one := &common.FrontLabelOption{
			Label: ns.Name,
			Value: ns.Name,
		}
		res = append(res, one)
	}

	common.OkWithDetailed(res, "ok", c)
}
